package com.beiding.my.repository;

import com.beiding.codecreater.render.java.boframe.common.Quote;
import com.beiding.my.common.Be;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;

import java.lang.reflect.AnnotatedType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
public class RepositoryHolder {

    private List<JpaRepository> jpaRepositories;

    public RepositoryHolder(@Autowired(required = false) List<JpaRepository> jpaRepositories) {
        this.jpaRepositories = jpaRepositories;
        try {
            init();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //所有的be仓库
    private Map<Class, JpaRepository> beRepositoryMap = new HashMap<>();

    //所有的引用仓库
    private Map<Class, JpaRepository> quoteRepositoryMap = new HashMap<>();

    //be实体-引用实体
    private Map<Class, Class> beQuoteMap = new HashMap<>();

    public Class getQuoteType(Class beType) {
        return beQuoteMap.get(beType);
    }

    public JpaRepository getBeRepository(Class beType) {
        return beRepositoryMap.get(beType);
    }

    public JpaRepository getQuoteRepository(Class beType) {
        return quoteRepositoryMap.get(beType);
    }

    //获取所有的be类型
    public Set<Class> getAllDtType() {
        return beRepositoryMap.keySet();
    }

    private void init() throws Exception {

        if (jpaRepositories != null) {
            for (JpaRepository qd : jpaRepositories) {
                Class<?>[] interfaces = qd.getClass().getInterfaces();
                for (Class<?> anInterface : interfaces) {
                    if (anInterface.isInterface()) {
                        AnnotatedType[] annotatedInterfaces = anInterface.getAnnotatedInterfaces();
                        for (AnnotatedType annotatedInterface : annotatedInterfaces) {
                            Type type = annotatedInterface.getType();
                            if (type instanceof ParameterizedType) {
                                ParameterizedType parameterizedType = (ParameterizedType) type;
                                if (parameterizedType.getRawType() == JpaRepository.class) {
                                    Class clz = (Class) parameterizedType.getActualTypeArguments()[0];
                                    String simpleName = clz.getSimpleName();
                                    if (simpleName.startsWith("_Quote_")) {

                                        //反射得到be类型
                                        String beClassName = clz.getPackage().getName() + "." + simpleName.substring(7);
                                        Class beType = Class.forName(beClassName);
                                        beQuoteMap.put(beType, clz);
                                        quoteRepositoryMap.put(beType, qd);
                                    } else {
                                        beRepositoryMap.put(clz, qd);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

    }


}
